package com.zxy.supplier_system.server.xinyuan.controller

import com.zxy.supplier_system.server.utils.IntIdAndName
import com.zxy.supplier_system.server.xinyuan.entity.XinyuanGoods
import com.zxy.supplier_system.server.xinyuan.service.XinyuanGoodsService
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/xinyuan/goods")
class XinyuanGoodsController(
    private val goodsService: XinyuanGoodsService
) {

    // ==================================================================
    // 创建商品
    // ==================================================================
    data class CreateGoodsRequest(
        val model: String,
        val categoryId: Int,
        val brandId: Int,
        val barcode: String,
        val energyEfficiencyLevel: XinyuanGoods.EnergyEfficiencyLevel? = null
    )

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    fun createGoods(
         @RequestBody request: CreateGoodsRequest
    ): GoodsDetailResponse {
        return goodsService.createGoods(request)
    }

    data class QueryGoodsListRequest(
        val categoryIds: List<Int>,
        val brandId: List<Int>,
        val barcode: String?,
        val model: String?,
    )

    // ==================================================================
    // 获取商品列表（带动态查询）
    // ==================================================================
    @GetMapping
    fun getGoodsList(
        request: QueryGoodsListRequest,
        pageable: Pageable
    ): Page<GoodsSummaryResponse> {
        return goodsService.getGoodsList(request, pageable)
    }

    data class GoodsSummaryResponse(
        val id: Int,
        val model: String,
        val category: IntIdAndName,
        val brand: IntIdAndName,
        val barcode: String,
    )

    @GetMapping(params = ["keyword"])
    fun searchByKeyword(keyword: String,isHomeAppliance: Boolean):List<SearchGoodsResponse>{
        return  this.goodsService.searchByKeyword(keyword, isHomeAppliance)
    }

    data class SearchGoodsResponse(
        val id:Int,
        val model: String,
        val category: IntIdAndName,
        val brand: IntIdAndName,
    )

    // ==================================================================
    // 获取商品详情
    // ==================================================================
    @GetMapping("/{id}")
    fun getGoodsDetail(@PathVariable id: Int): GoodsDetailResponse {
        return goodsService.getGoodsDetail(id)
    }

    data class CategoryResponse(
        val id: Int,
        val name: String
    )

    data class BrandResponse(
        val id: Int,
        val name: String
    )

    data class GoodsDetailResponse(
        val id: Int,
        val model: String,
        val category: CategoryResponse,
        val brand: BrandResponse,
        val barcode: String,
        val energyEfficiencyLevel: String?
    )

    // ==================================================================
    // 更新商品
    // ==================================================================
    data class UpdateGoodsRequest(
        val model: String? = null,
        val categoryId: Int? = null,
        val brandId: Int? = null,
        val barcode: String? = null,
        val energyEfficiencyLevel: XinyuanGoods.EnergyEfficiencyLevel? = null
    )

    @PutMapping("/{id}")
    fun updateGoods(
        @PathVariable id: Int,
     @RequestBody request: UpdateGoodsRequest
    ): GoodsDetailResponse {
        return goodsService.updateGoods(id, request)
    }

    // ==================================================================
    // 删除商品
    // ==================================================================
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    fun deleteGoods(@PathVariable id: Int) {
        goodsService.deleteGoods(id)
    }
}